<div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-label="Close">
        <span aria-hidden="true">&times;</span>
    </button>
    <h4 class="modal-title">添加用户</h4>
</div>
<div class="modal-body">
    <div class="page-content">
        <div class="table-responsive page-content">
            <form class="form-horizontal" role="form" id="user-info-operate-form">
                <input type="hidden" name="id" id="id" value="${(user.id)!''}">
                <div class="form-group">
                    <label class="col-sm-3 control-label no-padding-right" for="loginName">
                        登录名
                    </label>
                    <div class="col-sm-9">
                        <input type="text" id="loginName" name="loginName"
                               value="${(user.loginName)!''}"
                               placeholder="请填写用户登录名.."
                               class="col-xs-10 col-sm-5" <#if user?exists> disabled </#if>/>
                    </div>
                </div>
                <div class="form-group">
                    <label class="col-sm-3 control-label no-padding-right" for="realName">
                        真实名称
                    </label>
                    <div class="col-sm-9">
                        <input type="text" id="realName" name="realName"
                               value="${(user.realName)!''}"
                               placeholder="请填写用户真实名称.."
                               class="col-xs-10 col-sm-5"/>
                    </div>
                </div>
                <#if !user?exists>
                    <div class="form-group">
                        <label class="col-sm-3 control-label no-padding-right" for="loginPwd">
                            密码
                        </label>
                        <div class="col-sm-9">
                            <input type="password" id="loginPwd" name="loginPwd"
                                   value="" placeholder="请填写登录密码.."
                                   class="col-xs-10 col-sm-5"/>
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label no-padding-right" for="loginPwdConfirm">
                            确认密码
                        </label>
                        <div class="col-sm-9">
                            <input type="password" id="loginPwdConfirm" name="loginPwdConfirm"
                                   value="" placeholder="请填写确认密码.."
                                   class="col-xs-10 col-sm-5"/>
                        </div>
                    </div>
                </#if>
                <div class="form-group">
                    <label class="col-sm-3 control-label no-padding-right" for="phoneNo">
                        电话号码
                    </label>
                    <div class="col-sm-9">
                        <input type="text" id="phoneNo" name="phoneNo"
                               value="${(user.phoneNo)!''}"
                               placeholder="请填写用户电话号码.."
                               class="col-xs-10 col-sm-5"/>
                    </div>
                </div>
            </form>
        </div>
    </div>
</div>
<div class="modal-footer">
    <button class="btn btn-info" type="button" id="submitUserInfo">
        <i class="icon-ok bigger-110"></i>
        确认
    </button>
    <button class="btn" type="button" id="cancel">
        <i class="icon-undo bigger-110"></i>
        重置
    </button>
</div>

<script>
    $('#submitUserInfo').click(function () {
        $('#user-info-operate-form').submit();
    });

    //验证不能有中文
    jQuery.validator.addMethod("checkHasCHN", function (value, element) {
        var reg = /[\u4E00-\u9FA5]/g;
        if (reg.test(value)) {
            return false;
        } else {
            return true;
        }
    }, "不能包含中文字符");

    jQuery.validator.addMethod("mobilePhone", function (value, element) {
        return this.optional(element) || /^(13|15|18)\d{9}$/.test(value);
    }, "请输入正确的手机号码！");

    var validator = $('#user-info-operate-form').validate({
        // 错误展示位置
        errorPlacement: function (error, element) {
            $(element).parent().append(error);
        },
        errorElement: 'label',
        errorClass: 'help-inline',
        focusInvalid: true,
        onfocusout: false,
        rules: {
            realName: {
                required: true
            },
            loginName: {
                required: true
            },
            phoneNo: {
                required: true,
                mobilePhone: true
            }
        },
        messages: {
            loginName: {
                required: '请填写登录名'
            },
            realName: {
                required: '请填写真实名称'
            },
            phoneNo: {
                required: '请填写电话号码'
            }
        },
        invalidHandler: function (event, validator) { //display error alert on form submit
            $('.alert-danger', $('#validation-form')).show();
        },
        highlight: function (e) {
            $(e).closest('.form-group').removeClass('has-info').addClass('has-error');
        },
        success: function (e) {
            $(e).closest('.form-group').removeClass('has-error').addClass('has-info');
            $(e).remove();
        },
        errorPlacement: function (error, element) {
            error.insertAfter(element);
        },
        submitHandler: function (form) {
            //进行ajax传值
            $.ajax({
                url: "doOperate.do",
                type: "post",
                dataType: "json",
                data: $(form).serializeJson(),
                success: function (msg) {
                    //要执行的代码
                    if (!msg.success) {
                        layer.msg(msg.msg);
                        return;
                    }
                    $(parent.document).find("iframe").each(function () {
                        if ($(this).attr('src').indexOf("user/list.htm") > -1) {
                            $(this).attr('src', $(this).attr('src'));
                            reinitTreeHight(this);
                        }
                    });
                }
            });
            return false;
        }
    });

    if ($('#id').val() == '') {
        $('#loginPwd').rules('add', {
            required: true,
            checkHasCHN: true,
            minlength: 6,
            messages: {
                required: '请填写密码.',
                minlength: "密码最少6位"
            }
        });

        $('#loginPwdConfirm').rules('add', {
            required: true,
            checkHasCHN: true,
            minlength: 6,
            equalTo: '#loginPwd',
            messages: {
                required: '请填写密码.',
                equalTo: "两次密码不一致",
                minlength: "密码最少6位"
            }
        });
    }

    $("#cancel").click(function (e) {
        $('.form-group').removeClass('has-error');
        $('.form-group').removeClass('has-info');
        validator.resetForm();
        $('#user-info-operate-form')[0].reset();
    });
</script>